package com.apes.scm.masterdata.party.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * 功能：当事人关系
 *
 * @author xul
 * @create 2018-01-23 14:10
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PartyRelationship.CONST_TABLE_NAME
     // , indexes = {@Index(name = "IDX_PARTY_RELATION_U", columnList = "RELATIONSHIP_TYPE_ID, TO_PARTY_ROLE_ID", unique = true)}
)
public class PartyRelationship extends BaseModel {

    // todo 创建 关系类型 + formPartyRole + toPartyRole 的唯一索引

    public static final String CONST_TABLE_NAME = "mas_party_relationship";

    @Id
    @Column(name = "ID")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "当事人关系")
    private long id;

    @Column(name = "RELATIONSHIP_TYPE_ID", nullable = false, length = 20)
    private String relationshipTypeId; //关系类型

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "FROM_PARTY_ROLE_ID", referencedColumnName = "ID")
    private Party fromPartyRole; //当事人

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "TO_PARTY_ROLE_ID", referencedColumnName = "ID")
    private Party toPartyRole; //当事人

//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "organize_id", referencedColumnName = "id")
//    private Party organize; //默认组织（采购部门、销售部门）
}
